package com.gtr.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gtr.entity.VaccinumCategory;
import com.gtr.entity.VaccinumInfo;
import com.gtr.exception.appointException;
import com.gtr.mapper.VaccinumCategoryMapper;
import com.gtr.mapper.VaccinumInfoMapper;
import com.gtr.result.ResponseEnum;
import com.gtr.service.VaccinumInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gtr.vo.SellerVaccinumInfoVO;
import com.gtr.vo.SellerVaccinumInfoVO2;
import com.gtr.vo.SellerVaccinumInfoVO3;
import com.gtr.vo.VaccinumExcelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 疫苗表 服务实现类
 * </p>
 *
 * @author gtr
 * @since 2023-02-27
 */
@Service
@Slf4j
public class VaccinumInfoServiceImpl extends ServiceImpl<VaccinumInfoMapper, VaccinumInfo> implements VaccinumInfoService {

    @Autowired
    VaccinumInfoMapper vaccinumInfoMapper;

    @Autowired
    VaccinumCategoryMapper vaccinumCategoryMapper;

    private ReentrantLock reentrantLock = new ReentrantLock();

    @Override
    public BigDecimal findPriceById(Integer id) {
        return this.vaccinumInfoMapper.findPriceById(id);
    }

    @Override
    public Boolean subStockById(Integer id, Integer quantity) {//减库存
        //先判断库存是否正确
        Integer stock = this.vaccinumInfoMapper.findStockById(id);
        if(stock < quantity) {
            //抛异常
            throw new appointException(ResponseEnum.STOCK_ERROR.getMsg());
        }
        reentrantLock.lock(); //上锁
        Integer result = stock - quantity;
        this.vaccinumInfoMapper.updateStock(id, result);
        reentrantLock.unlock(); //解锁
        return true;
    }

    @Override
    public Boolean addStock(Integer id, Integer quantity) { //加库存
        this.vaccinumInfoMapper.addStock(id, quantity);
        return true;
    }

    @Override
    public SellerVaccinumInfoVO2 sellerVaccinumInfoVO(Integer page, Integer size) {
        Page<VaccinumInfo> infoPage = new Page<>(page, size);
        Page<VaccinumInfo> vaccinumInfoPage = this.vaccinumInfoMapper.selectPage(infoPage, null);
        SellerVaccinumInfoVO2 vo = new SellerVaccinumInfoVO2();
        vo.setSize(vaccinumInfoPage.getSize());
        vo.setTotal(vaccinumInfoPage.getTotal());
        List<VaccinumInfo> records = vaccinumInfoPage.getRecords();
        List<SellerVaccinumInfoVO> voList = new ArrayList<>();
        for (VaccinumInfo record : records) {
            SellerVaccinumInfoVO vo1 = new SellerVaccinumInfoVO();
            vo1.setId(record.getVaccinumId());
            vo1.setPrice(record.getVaccinumPrice());
            vo1.setStock(record.getVaccinumStock());
            if(record.getVaccinumStatus() == 1) {
                vo1.setStatus(true);
            } else {
                vo1.setStatus(false);
            }
            vo1.setName(record.getVaccinumName());
            vo1.setIcon(record.getVaccinumIcon());
            vo1.setDescription(record.getVaccinumDescription());
            QueryWrapper<VaccinumCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            VaccinumCategory vaccinumCategory = this.vaccinumCategoryMapper.selectOne(queryWrapper);
            vo1.setCategoryName(vaccinumCategory.getCategoryName());
            voList.add(vo1);
        }
        vo.setContent(voList);
        return vo;
    }

    @Override
    public SellerVaccinumInfoVO2 sellerVaccinumInfoVOLike(String keyWord, Integer page, Integer size) {
        Page<VaccinumInfo> infoPage = new Page<>(page, size);
        QueryWrapper<VaccinumInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("vaccinum_name", keyWord);
        Page<VaccinumInfo> vaccinumInfoPage = this.vaccinumInfoMapper.selectPage(infoPage, queryWrapper1);
        SellerVaccinumInfoVO2 vo = new SellerVaccinumInfoVO2();
        vo.setSize(vaccinumInfoPage.getSize());
        vo.setTotal(vaccinumInfoPage.getTotal());
        List<VaccinumInfo> records = vaccinumInfoPage.getRecords();
        List<SellerVaccinumInfoVO> voList = new ArrayList<>();
        for (VaccinumInfo record : records) {
            SellerVaccinumInfoVO vo1 = new SellerVaccinumInfoVO();
            vo1.setStock(record.getVaccinumStock());
            vo1.setId(record.getVaccinumId());
            vo1.setPrice(record.getVaccinumPrice());
            if(record.getVaccinumStatus() == 1) {
                vo1.setStatus(true);
            } else {
                vo1.setStatus(false);
            }
            vo1.setName(record.getVaccinumName());
            vo1.setIcon(record.getVaccinumIcon());
            vo1.setDescription(record.getVaccinumDescription());
            QueryWrapper<VaccinumCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            VaccinumCategory vaccinumCategory = this.vaccinumCategoryMapper.selectOne(queryWrapper);
            vo1.setCategoryName(vaccinumCategory.getCategoryName());
            voList.add(vo1);
        }
        vo.setContent(voList);
        return vo;
    }

    @Override
    public SellerVaccinumInfoVO2 sellerVaccinumInfoVOByCategoryType(Integer categoryType, Integer page, Integer size) {
        Page<VaccinumInfo> infoPage = new Page<>(page, size);
        QueryWrapper<VaccinumInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_type", categoryType);
        Page<VaccinumInfo> vaccinumInfoPage = this.vaccinumInfoMapper.selectPage(infoPage, queryWrapper1);
        SellerVaccinumInfoVO2 vo = new SellerVaccinumInfoVO2();
        vo.setSize(vaccinumInfoPage.getSize());
        vo.setTotal(vaccinumInfoPage.getTotal());
        List<VaccinumInfo> records = vaccinumInfoPage.getRecords();
        List<SellerVaccinumInfoVO> voList = new ArrayList<>();
        for (VaccinumInfo record : records) {
            SellerVaccinumInfoVO vo1 = new SellerVaccinumInfoVO();
            vo1.setStock(record.getVaccinumStock());
            vo1.setId(record.getVaccinumId());
            vo1.setPrice(record.getVaccinumPrice());
            if(record.getVaccinumStatus() == 1) {
                vo1.setStatus(true);
            } else {
                vo1.setStatus(false);
            }
            vo1.setName(record.getVaccinumName());
            vo1.setIcon(record.getVaccinumIcon());
            vo1.setDescription(record.getVaccinumDescription());
            QueryWrapper<VaccinumCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            VaccinumCategory vaccinumCategory = this.vaccinumCategoryMapper.selectOne(queryWrapper);
            vo1.setCategoryName(vaccinumCategory.getCategoryName());
            voList.add(vo1);
        }
        vo.setContent(voList);
        return vo;
    }

    @Override
    public SellerVaccinumInfoVO3 sellerVaccinumInfoVOById(Integer id) {
        VaccinumInfo vaccinumInfo = this.vaccinumInfoMapper.selectById(id);
        SellerVaccinumInfoVO3 vo1 = new SellerVaccinumInfoVO3();
        vo1.setStock(vaccinumInfo.getVaccinumStock());
        vo1.setId(vaccinumInfo.getVaccinumId());
        vo1.setPrice(vaccinumInfo.getVaccinumPrice());
        if(vaccinumInfo.getVaccinumStatus() == 1) {
            vo1.setStatus(true);
        } else {
            vo1.setStatus(false);
        }
        vo1.setName(vaccinumInfo.getVaccinumName());
        vo1.setIcon(vaccinumInfo.getVaccinumIcon());
        vo1.setDescription(vaccinumInfo.getVaccinumDescription());
        Map<String,Integer> map = new HashMap<>();
        map.put("categoryType",vaccinumInfo.getCategoryType());
        vo1.setCategory(map);
        return vo1;
    }

    @Override
    public List<VaccinumExcelVO> excelVOList() {
        List<VaccinumInfo> vaccinumInfoList = this.vaccinumInfoMapper.selectList(null);
        List<VaccinumExcelVO> vaccinumExcelVOList = new ArrayList<>();
        for (VaccinumInfo vaccinumInfo : vaccinumInfoList) {
            VaccinumExcelVO vo = new VaccinumExcelVO();
            BeanUtils.copyProperties(vaccinumInfo, vo);
            if(vaccinumInfo.getVaccinumStatus() == 1){
                vo.setVaccinumStatus("上架");
            } else {
                vo.setVaccinumStatus("下架");
            }
            vo.setCategoryName(this.vaccinumCategoryMapper.findNameByType(vaccinumInfo.getCategoryType()));
            vaccinumExcelVOList.add(vo);
        }
        return vaccinumExcelVOList;
    }

    @Override
    public List<VaccinumInfo> excleToVaccinumInfoList(InputStream inputStream) {
        try {
            List<VaccinumInfo> list = new ArrayList<>();
            EasyExcel.read(inputStream)
                    .head(VaccinumExcelVO.class)
                    .sheet()
                    .registerReadListener(new AnalysisEventListener<VaccinumExcelVO>() {

                        @Override
                        public void invoke(VaccinumExcelVO excelData, AnalysisContext analysisContext) {
                            VaccinumInfo vaccinumInfo = new VaccinumInfo();
                            BeanUtils.copyProperties(excelData, vaccinumInfo);
                            if(excelData.getVaccinumStatus().equals("正常")){
                                vaccinumInfo.setVaccinumStatus(1);
                            }else{
                                vaccinumInfo.setVaccinumStatus(0);
                            }
                            list.add(vaccinumInfo);
                        }

                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            log.info("=========================文件解析完成=========================");
                        }
                    }).doRead();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
